package com.itany.controller;


import com.github.pagehelper.PageInfo;
import com.itany.constant.StatusConst;
import com.itany.exception.ManagerServiceException;
import com.itany.pojo.ManagerUser;
import com.itany.service.ManagerUserService;
import com.itany.vo.ActionResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/managerUser")
public class ManagerUserController {

    @Autowired
    ManagerUserService service;

    @RequestMapping("/login")
    public ActionResult findOne(String username, String password, HttpSession httpSession) {

        ActionResult result = new ActionResult();

        try {
            ManagerUser user = service.login(username, password);
            httpSession.setAttribute(StatusConst.LOGIN_USER, user);
            result.setStatus(StatusConst.SUCCESS_RESPONSE);
            result.setMsg("成功");
        } catch (ManagerServiceException e) {
            result.setStatus(StatusConst.FAIL_RESPONSE);
            result.setMsg(e.getMessage());
        } catch (Exception e) {
            result.setStatus(StatusConst.FAIL_RESPONSE);
            result.setMsg(e.getMessage());
        }
        return result;
    }

    @RequestMapping("/logout")
    public ActionResult logout(HttpSession session) {
        session.invalidate();
        ActionResult result = new ActionResult();
        result.setStatus(StatusConst.SUCCESS_RESPONSE);
        return result;
    }


    @RequestMapping("/getLoginUser")
    public ActionResult getLoginUser(HttpSession session) {
        ActionResult result = new ActionResult();
        result.setStatus(StatusConst.SUCCESS_RESPONSE);
        Object user = session.getAttribute(StatusConst.LOGIN_USER);
        result.setData(user);


        if (user == null) {
            result.setStatus(StatusConst.FAIL_RESPONSE);
            result.setMsg("未登录");
        }
        return result;
    }

    @RequestMapping("/modifyPassword")
    public ActionResult modifyPassword(String password, HttpSession session) {
        ActionResult result = new ActionResult();
        try {
            ManagerUser managerUser = (ManagerUser) session.getAttribute(StatusConst.LOGIN_USER);
            if (managerUser == null) {
                throw new Exception("没登陆呢");
            }
            service.modifyPassword(managerUser.getId(), password);
            result.setStatus(StatusConst.SUCCESS_RESPONSE);
            result.setMsg(password);
        } catch (Exception e) {
            result.setStatus(StatusConst.FAIL_RESPONSE);
            result.setMsg(e.getMessage());
        }
        return result;
    }

    @RequestMapping("/add")
    public ActionResult add(String username,
                            String password,
                            Integer supplierid,
                            @RequestParam("ids[]") Integer[] ids) {
        ActionResult result = new ActionResult();
        try {
            service.add(username, password, supplierid, ids);
        } catch (Exception e) {
            result.setStatus(StatusConst.FAIL_RESPONSE);
            result.setMsg(e.getMessage());
        }
        return result;
    }

    @RequestMapping("/findAll")
    public Map<String, Object> findAll(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer rows,
            String username) {
        Map<String, Object> map = new HashMap<String, Object>();
        PageInfo<ManagerUser> info = service.findAll(page, rows, username);
        map.put("total", info.getTotal());
        map.put("rows", info.getList());
        return map;
    }


    @RequestMapping("/update")
    public ActionResult update(@RequestParam(required = true) Integer id,
                               String username,
                               String password,
                               Integer supplierid,
                               @RequestParam("ids[]") Integer[] ids) {
        ActionResult result = new ActionResult();
        try {
            service.update(id,username,password,supplierid,ids);
        } catch (Exception e) {
            result.setStatus(StatusConst.FAIL_RESPONSE);
            result.setMsg(e.getMessage());
        }
        return result;
    }



}
